Ext.ns("Morik.Office");
Morik.Office.CompanyPanel = function(config) {
	Morik.Office.CompanyPanel.superclass.constructor.call(this, config);
	var proxy = new Ext.data.DWRProxy( {
		api : {
			create : CompanyManager.addCompany,
			read : CompanyManager.getCompanys,
			update : CompanyManager.saveCompany,
			destroy : CompanyManager.removeCompany
		}
	});
	var recordType = new Ext.data.Record.create([ {
		name : "id",
		type : "int"
	}, {
		name : "comNum",
		type : "string"
	}, {
		name : "comName",
		type : "string"
	}, {
		name : "comAddress",
		type : "string"
	}]);	
	var reader = new Ext.data.DWRReader( {
		totalProperty : "totalSize",
		root : "data",
		id : "id"
	}, recordType);

	
	var ds = new Ext.data.Store( {
		autoSave:false,
		proxy : proxy,
		reader : reader,
		restful:true,
		writer:new Ext.data.DWRWriter()		
	});
	
	
	
	this.ds = ds;
	var cm = new Ext.grid.ColumnModel( {
		defaultSortable : true,
		defaultWidth : 180,
		columns : [ {
			header : '编号',
			dataIndex : 'comNum',
			editor : new Ext.form.TextField( {
				width : 180,
				allowBlank : false
			})
		}, {
			header : '名称',
			dataIndex : 'comName',
			editor : new Ext.form.TextField( {
				width : 180,
				allowBlank : false
			})
		}, {
			header : '公司地址',
			width : 200,
			dataIndex : 'comAddress',
			editor : new Ext.form.TextField( {
				width : 180,
				allowBlank : false
			})
		}]
	});

	var callback = function(r) {		
		alert("success!");
	};
	var exception = function(r) {
			alert("failure!");
	};
	var update = function(t, r, type) {
		if (type == Ext.data.Record.COMMIT) {
			CompanyManager.saveCompany(r.data, {
				callback : callback,
				exceptionHandler : exception
			});
		}
	};
	var remove = function(t, r, i) {
		CompanyManager.removeCompany(r.data);
	};

	var buttonSave = new Ext.Button( {
		text : '保存',
		handler : function(e) {	
			ds.save();
		}
	});
	var buttonReset = new Ext.Button( {
		text : '复位',
		handler : function() {			
			ds.rejectChanges();
		}
	});
	var buttonDelete = new Ext.Button( {
		text : '删除',
		handler : function(e) {			
			var d = function(btn, text) {
				var sm = grid.getSelectionModel()
				if (btn == 'yes') {
					var rec = sm.getSelected();
					if (!rec)
						return;
					ds.remove(rec);
				}
			}
			Ext.MessageBox.confirm("确定删除", "是否真的要删除？", d);
		}
	});
	var t = this;
	var buttonNew = new Ext.Button( {
		text : '新加',
		handler : function(e) {			
			var sm = grid.getSelectionModel();
			var rec = sm.getSelected();
			if (!rec)
				rec = ds.getAt(0);
			var c = rec.copy();
		//	c.data['_mark'] = 'add';
		//	delete c.data['id']; = 1000;
			var s = new (recordType)(Ext.apply(c.data, config || {}));
			ds.insert(0, [s]);
			grid.stopEditing();
			grid.startEditing(0, 1);		
		}
	});
	var pagingBar = new Ext.PagingToolbar( {
		pageSize : 10,
		store : ds,
		//displayInfo : true,
		//displayMsg : '共有 {2}，当前显示 {0} - {1}条',
		//emptyMsg : "没有数据",
		items : ['-', buttonSave, '-', buttonDelete, '-', buttonNew, '-',
				buttonReset]
	});
	this.pagingBar = pagingBar;
	 var sm = new Ext.grid.RowSelectionModel();

	var grid = new Ext.grid.EditorGridPanel( {
		cm : cm,
		sm : sm,
		store : ds,
		width : 570,
		height : 320,
		bbar : pagingBar,
		loadMask : {
			msg : '正在载入数据,请稍等...'
		},
		title : '公司列表'
	});
	 ds.load();	
	this.add(grid);
		

}
Ext.extend(Morik.Office.CompanyPanel, Ext.Panel);
Ext.onReady(function() {
	var p=new Morik.Office.CompanyPanel();
	p.render(Ext.getBody());
});
